package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private MinioTemplate minioTemplate;
    // 课程信息分页查询
    @PostMapping("/course/list")
    public ResponseResult findByPage(PageRequest pageRequest, @RequestBody CourseDto courseDto){
        ResponseResult result=  courseBaseService.findByPage(pageRequest,courseDto);
        return result;
    }
    // 课程封面上传
    @PostMapping("/course/upload")
    public ResponseResult upliad(MultipartFile uploadFile) throws IOException { //接收参数
        // 上传图片到minio
        String url = minioTemplate.upload(uploadFile.getInputStream(), uploadFile.getOriginalFilename(), uploadFile.getContentType());
        // 返回结果
        return ResponseResult.okResult(url);
    }

    // 课程新增
    @PostMapping("/course")
    public ResponseResult saveCoure(@RequestBody CourseBaseDto courseDto){ //接收参数
        // 调用方法 返回结果
        return courseBaseService.saveCoure(courseDto);
    }

    // 课程回显
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult findByBaseid(@PathVariable("courseBaseId") Long courseBaseId){    //接收参数

        //调用方法
        CourseBase base = courseBaseService.getById(courseBaseId); // 返回的结果不是咱们给前端返回的结果 要转一个格式
        // 转Vo
        CourseBaseVo baseVo = BeanHelper.copyProperties(base, CourseBaseVo.class);
        //  返回结果
        return ResponseResult.okResult(baseVo);
    }


    // 课程修改
    @PutMapping("/course")
    public ResponseResult updateCourse(@RequestBody CourseBaseDto courseBaseDto){ //接收参数
        // 调用方法 返回结果
        return courseBaseService.updateCourse(courseBaseDto);
    }
    // 课程删除
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteId(@PathVariable("courseId") Long courseId){ //接受参数
        // 调用方法 返回结果
         courseBaseService.deleteId(courseId);
         return ResponseResult.okResult();
    }
    // 平台审核
    @PostMapping("/course/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto){ //接受参数
        // 返回结果
        return courseBaseService.approve(dto);
    }
    // 提交审核
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult courseBaseId(@PathVariable("courseBaseId")Long courseBaseId){ //接收参数
        return courseBaseService.commit(courseBaseId);
    }

    // 课程下架
    @GetMapping("/course/offline/{courseId}")
    public ResponseResult DleteCourse(@PathVariable("courseId") Long courseId){ // 接收参数
        // 调用方法返回结果
        return  courseBaseService.DleteCourse(courseId);
    }












}
